﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace BLL
{
    public static class HelperClass
    {
        public static int GetEDDWeek(Nullable<DateTime> edd)
        {
            if (edd.HasValue)
            {
                return (280 + (DAL.ServerDateTime.SystemDateTime - edd.Value).Days) / 7;
            }
            else
            {
                return 0;
            }
        }

        public static double GetEDDM(Nullable<DateTime> edd)
        {
            if (edd.HasValue)
            {
                return Math.Round(9.333 - (edd.Value.Subtract(DAL.ServerDateTime.SystemDateTime).Days) / 30, 2);
            }
            else
            {
                return 0;
            }
        }

        public static string GetEDDS(Nullable<DateTime> edd)
        {
            string EDDS;
            if (edd.HasValue)
            {
                var EDDMtmp = Math.Round(9.333 - (edd.Value.Subtract(DAL.ServerDateTime.SystemDateTime).Days) / 30, 2);
                if (EDDMtmp > 9)
                {
                    EDDS = "BORN";
                }
                else
                {
                    if (EDDMtmp > 5.5)
                    {
                        EDDS = "TR3";
                    }
                    else
                    {
                        if (EDDMtmp > 2.5)
                        {
                            EDDS = "TR2";
                        }
                        else
                        {
                            EDDS = "TR1";
                        }
                    }
                }
            }
            else
            {
                EDDS = "BLANK";
            }
            return EDDS;
        }

        public static Nullable<DateTime> GetEDD(Nullable<int> week)
        {
            if (week.HasValue)
            {
                return DateTime.Parse(DAL.ServerDateTime.SystemDateTime.AddDays(280 - (7 * week.Value)).ToShortDateString());
            }
            else
            {
                return null;
            }
        }


        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }

        public static DateTime SystemDateTime
        {
            get{return DAL.ServerDateTime.SystemDateTime;}
        }

        public static string GetDuration(long second)
        {
            string time = null;

             if ((second / 3600) < 10)
            {
                time = "0" + (second / 3600).ToString() + " ";
            }
            else
            {
                time = (second / 3600).ToString() + " ";
            }


            second = second % 3600;

            if ((second / 60) < 10)
            {
                time = time + ": 0" + (second / 60).ToString() + " ";
            }
            else
            {
                time = time + ": " + (second / 60).ToString() + " ";
            }
            second = second % 60;


            if (second < 10)
            {
                time = time + ": 0" + second.ToString();
            }
            else
            {
                time = time + ": " + second.ToString();
            }

            return time;
        }

    }
}
